<?php

namespace app\api\business;

use app\api\model\Attribute;
use app\api\model\Images;
use app\api\model\Spec_goods;
use think\db\exception\DbEventException;
use think\facade\Db;

class GoodsBusiness
{
    /**
     * 添加商品及其对应的图片类型分类
     * @param $data
     * @return bool|\think\response\Json
     * @throws \think\exception\PDOException
     */
    public static function addGoods($data)
    {
        //  添加商品
        $res = \app\api\model\Goods::addGoods($data);

        return $goods_id =$res['id'];
    }
    public static function addImage($data,$goods_id){
        //添加图片
        $image=$data['goods_images'];
        $addImage=[];
        foreach ($image as $key=> $val){
            $addImage[$key]['goods_id']=$goods_id;
            $addImage[$key]['pics_big']=$val;
        }
        $obj=new Images();
        $obj->addImages($addImage);
    }

    public static function addSpecGoods($data,$goods_id){
        //          添加商品规格值
        $specGoods=[];
        $specGoodsInfo=$data['item'];
        foreach ($specGoodsInfo as $key=> $val){
            $specGoods[$key]['value_ids']=$val['value_ids'];
            $specGoods[$key]['value_names']=$val['value_names'];
            $specGoods[$key]['price']=$val['price'];
            $specGoods[$key]['cost_price']=$val['cost_price'];
            $specGoods[$key]['store_count']=$val['store_count'];
            $specGoods[$key]['goods_id']=$goods_id;
        }
        $obj=new Spec_goods();
        $obj->addSpecGoods($specGoods);

    }

    public static function addAttr($data){
        // 添加商品属性
        $goodsAttrs=$data['attr'];
        $addGoodsAttrs=[];
        foreach ($goodsAttrs as $key=> $val){
            $addGoodsAttrs[$key]['attr_name']=$val['attr_name'];
            $addGoodsAttrs[$key]['attr_values']=$val['attr_values'];

        }
        $obj=new Attribute();
        $obj->addAttrs($goodsAttrs);
    }



}